package com.gongpingjialoan.activity.search;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.DelearActivity;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.adapter.SearchCarAdapter;
import com.gongpingjialoan.adapter.SearchDealerAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.CarBean;
import com.gongpingjialoan.bean.DealerModel;
import com.gongpingjialoan.bean.HistoryModel;
import com.gongpingjialoan.bean.SearchDealerBean;
import com.gongpingjialoan.data.CarListFilterData;
import com.gongpingjialoan.data.CategoryData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.PreferenceUtils;
import com.gongpingjialoan.utility.SharedPreferencesUtil;
import com.gongpingjialoan.utility.UserLocation;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/9/12.
 */
public class TopSearchActivity extends BaseActivity implements View.OnClickListener, NetDataJson.OnStringListener, AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener<ListView>, PullToRefreshBase.OnLastItemVisibleListener {

    private NetDataJson mNetDataJson, getCountNetDataJson;

    private PullToRefreshListView pullToRefreshListView;

    private ImageView moreView;

    private TextView typeTextView, cheyuanTextView, cheshangTextView;

    private View popupView, footView;

    private String type = "1", value = "";

    private EditText mEditText;

    private PopupWindow popupWindow;

    private SearchCarAdapter mSearchAdapter;

    private ListView mListView, dealerListView;

    private List<CategoryData.SearchResult> mResultSet;

    private CategoryData mCategoryData;

    public int mPage;

    public boolean mIsLoadingMore = true;

    public boolean mHasMore;

    public boolean isRefresh;

    private SearchDealerBean mSearchDealerBean;

    private ImageView deleteCar, deleteDelear;

    private View hotView, history_view, historydealer_view, moreDomainView;

    private List<SearchDealerBean.DataEntity> mDataEntities;

    private SearchDealerAdapter mSearchDealerAdapter;

    private View errorView, seecarView, srcollView;

    private TagFlowLayout hotTagFlowLayout, historyTagFlowLayout, historydealer_flowlayout;

    private List<Map<String, String>> hotBrandList;

    private List<CarBean> carBeans;

    private Set<HistoryModel> mHistoryModels;

    private List<HistoryModel> mModels;

    private Set<DealerModel> mdealersModels;

    private List<DealerModel> dealers;

    private static final String MODEL_KEY = "MODEL_KEY";

    private static final String DEALERS_KEY = "DEALERS_KEY";

    private TagAdapter mTagAdapter;

    private JSONObject dataJsonObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "buy_dealer_search";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.top_search_activity);
        hotTagFlowLayout = (TagFlowLayout) findViewById(R.id.hot_flowlayout);
        historyTagFlowLayout = (TagFlowLayout) findViewById(R.id.history_flowlayout);
        historydealer_flowlayout = (TagFlowLayout) findViewById(R.id.historydealer_flowlayout);
        hotView = findViewById(R.id.hot_view);
        history_view = findViewById(R.id.history_view);
        historydealer_view = findViewById(R.id.historydealer_view);
        errorView = findViewById(R.id.error_layout);
        seecarView = findViewById(R.id.seecar);
        footView = LayoutInflater.from(this).inflate(R.layout.listview_footview, null);
        moreView = (ImageView) findViewById(R.id.more);
        typeTextView = (TextView) findViewById(R.id.type);
        deleteCar = (ImageView) findViewById(R.id.delete);
        srcollView = findViewById(R.id.scroll);
        moreDomainView = findViewById(R.id.more_domain);
        deleteDelear = (ImageView) findViewById(R.id.delete_dealer);
        popupView = LayoutInflater.from(this).inflate(R.layout.type_view, null);
        cheyuanTextView = (TextView) popupView.findViewById(R.id.cheyuan);
        cheshangTextView = (TextView) popupView.findViewById(R.id.cheshang);
        mEditText = (EditText) findViewById(R.id.edit);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ColorDrawable cd = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cd);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                moreView.setImageResource(R.drawable.jiantouxia1);
            }
        });


        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listview);
        pullToRefreshListView.setScrollingWhileRefreshingEnabled(false);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setOnLastItemVisibleListener(this);
        mNetDataJson = new NetDataJson(this, 1);

        mDataEntities = new ArrayList<>();
        mSearchDealerAdapter = new SearchDealerAdapter(mDataEntities, this);
        dealerListView = pullToRefreshListView.getRefreshableView();
        dealerListView.addFooterView(footView);
        dealerListView.setAdapter(mSearchDealerAdapter);
        dealerListView.setOnItemClickListener(this);
        dealerListView.removeFooterView(footView);


        mListView = (ListView) findViewById(R.id.list_cheyuan);
        mResultSet = new ArrayList<>();
        mSearchAdapter = new SearchCarAdapter(mResultSet, this);
        mListView.setAdapter(mSearchAdapter);
        moreView.setOnClickListener(this);
        typeTextView.setOnClickListener(this);
        mListView.setOnItemClickListener(this);
        cheyuanTextView.setOnClickListener(this);
        cheshangTextView.setOnClickListener(this);
        seecarView.setOnClickListener(this);
        deleteDelear.setOnClickListener(this);
        deleteCar.setOnClickListener(this);

        pullToRefreshListView.setVisibility(View.GONE);
        mListView.setVisibility(View.GONE);
        historydealer_view.setVisibility(View.GONE);

        getCountData();
        mCategoryData = ((GPJApplication) getApplication()).getCategoryData();
        hotBrandList = mCategoryData.mHotBrandList;
        getHotCars();

        String models = (String) SharedPreferencesUtil.get(this, MODEL_KEY, "");
        if (TextUtils.isEmpty(models)) {
            mModels = new ArrayList<>();
            mHistoryModels = new HashSet<>();
        } else {
            try {
                mHistoryModels = PreferenceUtils.String2ModelList(models);
                mModels = new ArrayList<>(new HashSet<>(mHistoryModels));
            } catch (Exception e) {
                mModels = new ArrayList<>();
                mHistoryModels = new HashSet<>();
            }
        }

        setAdapter();
        if (mModels.size() <= 0) {
            history_view.setVisibility(View.GONE);
        }

        String dealer = (String) SharedPreferencesUtil.get(this, DEALERS_KEY, "");
        if (TextUtils.isEmpty(dealer)) {
            dealers = new ArrayList<>();
            mdealersModels = new HashSet<>();
        } else {
            try {
                mdealersModels = PreferenceUtils.String2DealerList(dealer);
                dealers = new ArrayList<>(new HashSet<>(mdealersModels));
            } catch (Exception e) {
                dealers = new ArrayList<>();
                mdealersModels = new HashSet<>();
            }
        }

        mTagAdapter = new TagAdapter<DealerModel>(dealers) {
            @Override
            public View getView(FlowLayout parent, int position, DealerModel o) {
                TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv2,
                        historydealer_flowlayout, false);
                tv.setText(o.getDealer_name());
                return tv;
            }
        };
        historydealer_flowlayout.setAdapter(mTagAdapter);

        if (dealers.size() <= 0) {
            historydealer_view.setVisibility(View.GONE);
        }

        mEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                value = arg0.toString();
                if (type.equals("1")) {
                    if (!TextUtils.isEmpty(value)) {
                        mResultSet = mCategoryData.search(value, dataJsonObject);
                    } else {
                        mResultSet.clear();
                    }
                    mSearchAdapter.setData(mResultSet);
                    if (mResultSet.size() > 0) {
                        mListView.setVisibility(View.VISIBLE);
                    } else {
                        mListView.setVisibility(View.GONE);
                        hotView.setVisibility(View.VISIBLE);
                        if (mModels.size() > 0) {
                            history_view.setVisibility(View.VISIBLE);
                        } else {
                            history_view.setVisibility(View.GONE);
                        }
                    }
                } else if (type.equals("2")) {
                    if (!TextUtils.isEmpty(value)) {
                        myHandler.removeMessages(1);
                        myHandler.sendEmptyMessageDelayed(1, 1000);
                        pullToRefreshListView.setVisibility(View.VISIBLE);
                    } else {
                        myHandler.removeMessages(1);
                        mDataEntities.clear();
                        mSearchDealerAdapter.setData(mDataEntities);
                        pullToRefreshListView.setVisibility(View.GONE);
                        if (dealers.size() > 0) {
                            historydealer_view.setVisibility(View.VISIBLE);
                            historydealer_flowlayout.setVisibility(View.VISIBLE);
                        } else {
                            historydealer_view.setVisibility(View.GONE);
                            historydealer_flowlayout.setVisibility(View.GONE);
                        }
                    }

                }
            }
        });

        hotTagFlowLayout.setAdapter(new TagAdapter<CarBean>(carBeans) {
            @Override
            public View getView(FlowLayout parent, int position, CarBean o) {
                TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv2,
                        hotTagFlowLayout, false);
                tv.setText(o.brand_name);
                return tv;
            }
        });

        hotTagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //删除数据
                CarListFilterData.getInstance().clear();
                CarListFilterData.getInstance().setIsCleanData(true);
                CarListFilterData.getInstance().setBrandName(carBeans.get(position).brand_name);
                CarListFilterData.getInstance().setBrandSlug(carBeans.get(position).brand_slug);
                CarListFilterData.getInstance().setModelName(carBeans.get(position).modle_name);
                CarListFilterData.getInstance().setModelSlug(carBeans.get(position).modle_slug);

                //保存数据
                HistoryModel historyModel = new HistoryModel();
                historyModel.setBrandName(carBeans.get(position).brand_name);
                historyModel.setBrandSlug(carBeans.get(position).brand_slug);
                historyModel.setModelName(carBeans.get(position).modle_name);
                historyModel.setModelSlug(carBeans.get(position).modle_slug);
                mHistoryModels.add(historyModel);
                try {
                    SharedPreferencesUtil.put(TopSearchActivity.this, MODEL_KEY, PreferenceUtils.ModelList2String(mHistoryModels));
                } catch (Exception e) {
                }

                MainActivity.main.gotocheshi();
                finish();
                return true;
            }
        });

        historyTagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //删除数据
                CarListFilterData.getInstance().clear();
                CarListFilterData.getInstance().setIsCleanData(true);
                CarListFilterData.getInstance().setBrandName(mModels.get(position).getBrandName());
                CarListFilterData.getInstance().setBrandSlug(mModels.get(position).getBrandSlug());
                CarListFilterData.getInstance().setModelName(mModels.get(position).getModelName());
                CarListFilterData.getInstance().setModelSlug(mModels.get(position).getModelSlug());

                MainActivity.main.gotocheshi();
                finish();
                return true;
            }
        });

        historydealer_flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Intent intent = new Intent(TopSearchActivity.this, DelearActivity.class);
                intent.putExtra("domain", dealers.get(position).getDealer_name());
                intent.putExtra("dealer_id", dealers.get(position).getDealer_id());
                intent.putExtra("city", dealers.get(position).getCity());
                startActivity(intent);
                return true;
            }
        });
    }


    private void setAdapter() {
        historyTagFlowLayout.setAdapter(new TagAdapter<HistoryModel>(mModels) {
            @Override
            public View getView(FlowLayout parent, int position, HistoryModel o) {
                TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv2,
                        historyTagFlowLayout, false);
                if (TextUtils.isEmpty(o.getModelName())) {
                    tv.setText(o.getBrandName());
                } else {
                    tv.setText(o.getModelName());
                }
                return tv;
            }
        });
    }


    private void getHotCars() {
        if (hotBrandList == null || hotBrandList.size() <= 0) {
            hotView.setVisibility(View.GONE);
            return;
        }
        int size = hotBrandList.size();
        carBeans = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Map<String, String> map = hotBrandList.get(i);
            CarBean carBean = new CarBean();
            carBean.brand_name = map.get("name");
            carBean.brand_slug = map.get("slug");
            carBean.maxprice = map.get("max_price_bn");
            carBeans.add(carBean);
        }
    }


    @Override
    public void onClick(View v) {
        if (v == moreView || v == typeTextView) {
            moreView.setImageResource(R.drawable.jiantoushang1);
            popupWindow.showAsDropDown(typeTextView, 0, 5);
        } else if (v == cheyuanTextView) {
            type = "1";
            typeTextView.setText("车源");
            mEditText.setText("");
            hotView.setVisibility(View.VISIBLE);
            if (mModels.size() > 0) {
                history_view.setVisibility(View.VISIBLE);
            } else {
                history_view.setVisibility(View.GONE);
            }
            if (mResultSet.size() > 0) {
                mListView.setVisibility(View.VISIBLE);
            }
            srcollView.setVisibility(View.VISIBLE);
            moreDomainView.setVisibility(View.VISIBLE);
            pullToRefreshListView.setVisibility(View.GONE);
            historydealer_view.setVisibility(View.GONE);
            errorView.setVisibility(View.GONE);
            popupWindow.dismiss();
        } else if (v == cheshangTextView) {
            type = "2";
            typeTextView.setText("店铺");
            mEditText.setText("");
            srcollView.setVisibility(View.GONE);
            hotView.setVisibility(View.GONE);
            history_view.setVisibility(View.GONE);
            mListView.setVisibility(View.GONE);
            if (dealers.size() > 0) {
                historydealer_view.setVisibility(View.VISIBLE);
                historydealer_flowlayout.setVisibility(View.VISIBLE);
            } else {
                historydealer_view.setVisibility(View.GONE);
                historydealer_flowlayout.setVisibility(View.GONE);
            }
            if (mDataEntities.size() > 0) {
                pullToRefreshListView.setVisibility(View.VISIBLE);
            } else {
                pullToRefreshListView.setVisibility(View.GONE);
            }
            moreDomainView.setVisibility(View.GONE);
            errorView.setVisibility(View.GONE);
            popupWindow.dismiss();
        } else if (v == seecarView) {
            MainActivity.main.gotocheshi();
            finish();
        } else if (v == deleteDelear) {
            dealers.clear();
            mTagAdapter.notifyDataChanged();
            mdealersModels.clear();
            try {
                SharedPreferencesUtil.put(this, DEALERS_KEY, PreferenceUtils.DealersList2String(mdealersModels));
            } catch (Exception e) {
            }
            historydealer_view.setVisibility(View.GONE);
        } else if (v == deleteCar) {
            mModels.clear();
            mHistoryModels.clear();
            try {
                SharedPreferencesUtil.put(this, MODEL_KEY, PreferenceUtils.ModelList2String(mHistoryModels));
            } catch (Exception e) {
            }
            history_view.setVisibility(View.GONE);
        }
    }


    private void getdataFromDealer() {
//        mDataEntities.clear();
//        mSearchDealerAdapter.setData(mDataEntities);
//        mListView.setVisibility(View.GONE);

        errorView.setVisibility(View.GONE);
        pullToRefreshListView.setRefreshing();
    }


    @Override
    public void onDataJsonError(String errorMessage) {
        if (!isRefresh && mIsLoadingMore) {
            dealerListView.removeFooterView(footView);
        }
        mIsLoadingMore = false;
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
        pullToRefreshListView.onRefreshComplete();
    }

    @Override
    public void onDataJsonUpdate(String json) {
        if (!isRefresh && mIsLoadingMore) {
            dealerListView.removeFooterView(footView);
        }
        if (isRefresh) {
            mDataEntities.clear();
            mSearchDealerAdapter.setData(mDataEntities);
        }
        Gson gson = new Gson();
        mSearchDealerBean = gson.fromJson(json, SearchDealerBean.class);
        if ("success".equals(mSearchDealerBean.getStatus())) {
            mDataEntities.addAll(mSearchDealerBean.getData());
            int page_num = (int) mSearchDealerBean.getTotal_page();
            if (page_num == mPage) {
                mHasMore = false;
            }
            if (0 == mDataEntities.size()) {
                mIsLoadingMore = false;
                pullToRefreshListView.onRefreshComplete();
                mListView.setVisibility(View.GONE);
                dealerListView.setVisibility(View.GONE);
                errorView.setVisibility(View.VISIBLE);
                if (dealers.size() > 0) {
                    historydealer_view.setVisibility(View.VISIBLE);
                    historydealer_flowlayout.setVisibility(View.VISIBLE);
                } else {
                    historydealer_view.setVisibility(View.GONE);
                    historydealer_flowlayout.setVisibility(View.GONE);
                }
                return;
            }
            mIsLoadingMore = false;
            mSearchDealerAdapter.setData(mDataEntities);
            pullToRefreshListView.onRefreshComplete();
        } else {
            mIsLoadingMore = false;
            pullToRefreshListView.onRefreshComplete();
            return;
        }
        if (mDataEntities.size() > 0) {
            dealerListView.setVisibility(View.VISIBLE);
        } else {
            dealerListView.setVisibility(View.GONE);
            if (dealers.size() > 0) {
                historydealer_view.setVisibility(View.VISIBLE);
                historydealer_flowlayout.setVisibility(View.VISIBLE);
            } else {
                historydealer_view.setVisibility(View.GONE);
                historydealer_flowlayout.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent == mListView) {
            //删除数据
            CarListFilterData.getInstance().clear();
            CarListFilterData.getInstance().setBrandName(mResultSet.get(position).mBrandName);
            CarListFilterData.getInstance().setBrandSlug(mResultSet.get(position).mBrandSlug);
            CarListFilterData.getInstance().setModelName(mResultSet.get(position).mModelName);
            CarListFilterData.getInstance().setModelSlug(mResultSet.get(position).mModelSlug);

            //保存数据
            HistoryModel historyModel = new HistoryModel();
            historyModel.setBrandName(mResultSet.get(position).mBrandName);
            historyModel.setBrandSlug(mResultSet.get(position).mBrandSlug);
            historyModel.setModelName(mResultSet.get(position).mModelName);
            historyModel.setModelSlug(mResultSet.get(position).mModelSlug);
            mHistoryModels.add(historyModel);
            try {
                SharedPreferencesUtil.put(this, MODEL_KEY, PreferenceUtils.ModelList2String(mHistoryModels));
            } catch (Exception e) {
            }

            if (MainActivity.main != null)
                MainActivity.main.gotocheshi();
            finish();

        } else if (parent == dealerListView) {
            SearchDealerBean.DataEntity dataEntity = mDataEntities.get(position - dealerListView.getHeaderViewsCount());

            DealerModel dealerModel = new DealerModel();
            dealerModel.setCity(dataEntity.getCity());
            dealerModel.setDealer_id(dataEntity.getId() + "");
            dealerModel.setDealer_name(dataEntity.getCompany_name());
            mdealersModels.add(dealerModel);
            try {
                SharedPreferencesUtil.put(this, DEALERS_KEY, PreferenceUtils.DealersList2String(mdealersModels));
            } catch (Exception e) {
            }
            List<DealerModel> s = new ArrayList<>(new HashSet<>(mdealersModels));
            dealers.clear();
            dealers.addAll(s);
            mTagAdapter.notifyDataChanged();


            Intent intent = new Intent(this, DelearActivity.class);
            intent.putExtra("domain", dataEntity.getCompany_name());
            intent.putExtra("dealer_id", dataEntity.getId() + "");
            intent.putExtra("city", dataEntity.getCity());
            startActivity(intent);
        }
    }

    @Override
    public void onLastItemVisible() {
        if (!mHasMore) {
            return;
        }
        if (!mIsLoadingMore && mHasMore) {
            dealerListView.addFooterView(footView);
            dealerListView.setSelection(dealerListView.getBottom());
            isRefresh = false;
            mIsLoadingMore = true;
            mPage++;
            updateData();
        }
    }

    boolean isFristLoading = true;

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        if (!isFristLoading) {
            String label =
                    "更新时间:" + DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
        }
        isFristLoading = false;
        refresh();
    }


    private void refresh() {
        mPage = 1;
        mHasMore = true;
        isRefresh = true;
        updateData();
    }


    private void updateData() {

        mNetDataJson.cancelTask();
//        if(pullToRefreshListView.isRefreshing()){
//            pullToRefreshListView.onRefreshComplete();
//        }
        mNetDataJson.addParam("page", mPage);
        mNetDataJson.addParam("sub", value);
        if (UserLocation.getInstance().isIslocation()) {
            mNetDataJson.addParam("lat", UserLocation.getInstance().getLatitude());
            mNetDataJson.addParam("lng", UserLocation.getInstance().getLongitude());
        }
        mNetDataJson.setUrl(API.get_dealers);
        mNetDataJson.request("get");
    }


    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                getdataFromDealer();
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNetDataJson != null) {
            mNetDataJson.cancelTask();
            mNetDataJson = null;

        }
        if (getCountNetDataJson != null) {
            getCountNetDataJson.cancelTask();
            getCountNetDataJson = null;
        }
    }


    private void getCountData() {
        if (getCountNetDataJson == null) {
            getCountNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
                @Override
                public void onDataJsonError(String errorMessage) {

                }

                @Override
                public void onDataJsonUpdate(JSONObject json) {
                    try {
                        dataJsonObject = json.getJSONObject("data");
                    } catch (JSONException e) {
                    }
                }
            });
        }
        getCountNetDataJson.setUrl(API.getOnsaleCar);
        getCountNetDataJson.setCacheTime(60 * 60 * 12);
        getCountNetDataJson.addParam("city", MainActivity.main.currentCity);
        getCountNetDataJson.request("get");
    }


}
